//
//  WDWorkOrderCurrentCondView.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/9/8.
//

#import "WDWorkOrderCurrentCondView.h"
#import "UIWindow+WDWorkOrderCurrentCondView.h"
#import "WDWorkOrderViewModel.h"

@interface WDWorkOrderCurrentCondView ()
@property (nonatomic, strong) WDWorkOrderViewModel *viewModel;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) void(^completionBlock)(void);
@property (nonatomic, copy) void(^failedBlock)(void);

@property (nonatomic, strong) UIButton *btnClose;

/// 重置
@property (nonatomic, strong) UIButton *btnReset;

/// 确定
@property (nonatomic, strong) UIButton *btnSubmit;


/// 查询时间
@property (nonatomic, strong) UILabel *dateLabel;

/// 工单状态
@property (nonatomic, strong) UILabel *statusLabel;

/// 操作人
@property (nonatomic, strong) UILabel *operatorLabel;

/// 查询时间视图
@property (nonatomic, strong) NSMutableArray *dateViewList;

@property (nonatomic, strong) NSMutableArray *statusViewList;

@property (nonatomic, strong) NSMutableArray *operatorViewList;


- (void)initWithDateLabel;

- (void)initWithStatusLabel;

- (void)initWithOperatorLabel;



/// 重置
- (void)initWithBtnReset;

/// 确定
- (void)initWithBtnSubmit;

@end

@implementation WDWorkOrderCurrentCondView

- (void)initWithSubView
{
    self.backgroundColor = kWhiteColor;
        
    _dateViewList = [NSMutableArray new];
    
    _statusViewList = [NSMutableArray new];
    
    _operatorViewList = [NSMutableArray new];
    
//    [self initWithBtnClose];
}

- (void)initWithBtnClose {
    if (!_btnClose) {
        CGRect frame = CGRectMake(self.width - 4 - 30, 4, 30, 30);
        UIImage *image = [UIImage imageNamed:@"cs_ic_delet_small"];
        _btnClose = [UIButton createWithButton:self frame:frame font:FONTSIZE(16) target:self action:@selector(actionWithClose:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setImage:image forState:UIControlStateNormal];
        }];
    }
}

/// 重置
- (void)initWithBtnReset {
    if (!_btnReset) {
        CGRect frame = CGRectMake(20, self.height - 18 - 48, (kScreenW - 20 * 2 - 16) * 0.5, 48);
        _btnReset = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithReset:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"重置" forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateNormal];
            button.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = YES;
        }];
    }
}

/// 确定
- (void)initWithBtnSubmit {
    if (!_btnSubmit) {
        CGRect frame = self.btnReset.frame;
        frame.origin.x = self.btnReset.right + 16;
        _btnSubmit = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithSubmit:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:@"确定" forState:UIControlStateNormal];
            [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
            button.backgroundColor = kThemeColor;
            button.layer.cornerRadius = 5;
            button.layer.masksToBounds = YES;
        }];
    }
}

- (void)initWithDateLabel {
    if (!_dateLabel) {
        CGRect frame = CGRectMake(20, 10, self.width - 40, 20);
        _dateLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"查询时间";
        }];
    }
}

- (void)initWithDateViews {
    if (self.dateViewList.count > 0) {
        return;
    }
    
    float leftSpace = 20;
    float midSpace = 15;
    float width = (kScreenW - leftSpace * 2 - midSpace) * 0.5;
    float height = 38;
    UIButton *btnPre = nil;
    CGRect frame = CGRectMake(leftSpace, self.dateLabel.bottom + 10, width, height);
    for (int i=0; i<self.viewModel.currentDates.count; i++) {
        TYZCommonCellModel *model = self.viewModel.currentDates[i];
        if (i % 2 == 0) {
            if (btnPre) {
                frame.origin.y = btnPre.bottom + midSpace;
                frame.origin.x = leftSpace;
            }
        } else {
            frame.origin.x = btnPre.right + midSpace;
        }
        UIButton *btnDate = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithDate:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:model.title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateSelected];
            button.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
            button.layer.borderWidth = 1;
        }];
        [self.dateViewList addObject:btnDate];
        btnPre = btnDate;
    }
    
}

- (void)initWithStatusLabel {
    if (!_statusLabel) {
        UIButton *btnLast = self.dateViewList.lastObject;
        CGRect frame = self.dateLabel.frame;
        frame.origin.y = btnLast.bottom + 15;
        _statusLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"工单状态";
        }];
    }
}

- (void)initWithStatusViews {
    if (self.statusViewList.count > 0) {
        return;
    }
    
    float leftSpace = 20;
    float midSpace = 15;
    float width = (kScreenW - leftSpace * 2 - midSpace) * 0.5;
    float height = 38;
    UIButton *btnPre = nil;
    CGRect frame = CGRectMake(leftSpace, self.statusLabel.bottom + 10, width, height);
    for (int i=0; i<self.viewModel.currentOrderStatus.count; i++) {
        TYZCommonCellModel *model = self.viewModel.currentOrderStatus[i];
        if (i % 2 == 0) {
            if (btnPre) {
                frame.origin.y = btnPre.bottom + midSpace;
                frame.origin.x = leftSpace;
            }
        } else {
            frame.origin.x = btnPre.right + midSpace;
        }
        UIButton *btnDate = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithStatus:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:model.title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateSelected];
            button.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
            button.layer.borderWidth = 1;
        }];
        [self.statusViewList addObject:btnDate];
        btnPre = btnDate;
    }
}

- (void)initWithOperatorLabel {
    if (!_operatorLabel) {
        UIButton *btnLast = self.statusViewList.lastObject;
        CGRect frame = self.dateLabel.frame;
        frame.origin.y = btnLast.bottom + 15;
        _operatorLabel = [UILabel createWithLabel:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] propertiesBlock:^(UILabel * _Nonnull label) {
            label.textColor = [UIColor colorWithHexString:@"#000000"];
            label.text = @"操作人";
        }];
    }
}

- (void)initWithOperatorViews {
    if (self.operatorViewList.count > 0) {
        return;
    }
    
    float leftSpace = 20;
    float midSpace = 15;
    float width = (kScreenW - leftSpace * 2 - midSpace) * 0.5;
    float height = 38;
    UIButton *btnPre = nil;
    CGRect frame = CGRectMake(leftSpace, self.operatorLabel.bottom + 10, width, height);
    for (int i=0; i<self.viewModel.currentOperators.count; i++) {
        TYZCommonCellModel *model = self.viewModel.currentOperators[i];
        if (i % 2 == 0) {
            if (btnPre) {
                frame.origin.y = btnPre.bottom + midSpace;
                frame.origin.x = leftSpace;
            }
        } else {
            frame.origin.x = btnPre.right + midSpace;
        }
        UIButton *btnDate = [UIButton createWithButton:self frame:frame font:[UIFont systemFontOfSize:14 weight:UIFontWeightRegular] target:self action:@selector(actionWithOperator:) propertiesBlock:^(UIButton * _Nonnull button) {
            [button setTitle:model.title forState:UIControlStateNormal];
            [button setTitleColor:[UIColor colorWithHexString:@"#000000"] forState:UIControlStateNormal];
            [button setTitleColor:kThemeColor forState:UIControlStateSelected];
            button.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
            button.layer.borderWidth = 1;
        }];
        [self.operatorViewList addObject:btnDate];
        btnPre = btnDate;
    }
}


/// 查询时间
/// - Parameter snder: snder description
- (void)actionWithDate:(id)sender {
    UIButton *btnCur = (UIButton *)sender;
    btnCur.selected = !btnCur.selected;
    for (int i=0; i<self.dateViewList.count; i++) {
        UIButton *btn = self.dateViewList[i];
        TYZCommonCellModel *model = self.viewModel.currentDates[i];
        if ([btn isEqual:btnCur]) {
            if (btnCur.selected) {
                btn.layer.borderColor = kThemeColor.CGColor;
            } else {
                btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
            }
        } else {
            btn.selected = NO;
            btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
        }
        model.isCheck = btn.selected;
    }
}

- (void)actionWithStatus:(id)sender {
    UIButton *btnCur = (UIButton *)sender;
    btnCur.selected = !btnCur.selected;
    for (int i=0; i<self.statusViewList.count; i++) {
        UIButton *btn = self.statusViewList[i];
        TYZCommonCellModel *model = self.viewModel.currentOrderStatus[i];
        if ([btn isEqual:btnCur]) {
            if (btnCur.selected) {
                btn.layer.borderColor = kThemeColor.CGColor;
            } else {
                btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
            }
        } else {
            btn.selected = NO;
            btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
        }
        model.isCheck = btn.selected;
    }
}

/// 操作
/// - Parameter sender: sender description
- (void)actionWithOperator:(id)sender {
    UIButton *btnCur = (UIButton *)sender;
    btnCur.selected = !btnCur.selected;
    for (int i=0; i<self.operatorViewList.count; i++) {
        UIButton *btn = self.operatorViewList[i];
        TYZCommonCellModel *model = self.viewModel.currentOperators[i];
        if ([btn isEqual:btnCur]) {
            if (btnCur.selected) {
                btn.layer.borderColor = kThemeColor.CGColor;
            } else {
                btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
            }
        } else {
            btn.selected = NO;
            btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
        }
        model.isCheck = btn.selected;
    }
}

/// 重置
/// - Parameter sender: sender description
- (void)actionWithReset:(id)sender {
    for (int i=0; i<self.dateViewList.count; i++) {
        UIButton *btn = self.dateViewList[i];
        TYZCommonCellModel *model = self.viewModel.currentDates[i];
        btn.selected = NO;
        btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
        model.isCheck = NO;
    }
    
    for (int i=0; i<self.statusViewList.count; i++) {
        UIButton *btn = self.statusViewList[i];
        TYZCommonCellModel *model = self.viewModel.currentOrderStatus[i];
        btn.selected = NO;
        btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
        model.isCheck = NO;
    }
    
    for (int i=0; i<self.operatorViewList.count; i++) {
        UIButton *btn = self.operatorViewList[i];
        TYZCommonCellModel *model = self.viewModel.currentOperators[i];
        btn.selected = NO;
        btn.layer.borderColor = [UIColor colorWithHexString:@"#CBCED4"].CGColor;
        model.isCheck = NO;
    }
}

/// 确定
/// - Parameter sender: sender description
- (void)actionWithSubmit:(id)sender {
    [self clickedSubmit:nil];
}


- (void)showWithTitle:(NSString *)title data:(id)data completion:(void (^)(void))completionBlock failed:(void (^)(void))failedBlock
{
    self.title = title;
    self.viewModel = data;
    self.completionBlock = completionBlock;
    self.failedBlock = failedBlock;
    if (!kCSKeyWindow.ordCurCondView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.ordCurCondMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.ordCurCondView = self;
        
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickedSubmit:)];
//        [maskView addGestureRecognizer:tap];
//        [self addGestureRecognizer:tap];
        
        [UIView animateWithDuration:0.2 animations:^{
            maskView.alpha = 0.4;
//            maskView.alpha = 0;
            self.alpha = 1;
        }];
    }

    /// 重置
    [self initWithBtnReset];

    /// 确定
    [self initWithBtnSubmit];
    
    [self initWithDateLabel];
    [self initWithDateViews];

    [self initWithStatusLabel];
    [self initWithStatusViews];

    [self initWithOperatorLabel];
    [self initWithOperatorViews];

}

- (void)clickedCancel:(id)sender
{
    if (self.failedBlock)
    {
        self.failedBlock();
    }
}

- (void)clickedSubmit:(id)sender
{
    if (self.completionBlock)
    {
        self.completionBlock();
    }
}

- (void)actionWithClose:(id)sender {
    [self actionWithReset:nil];
    
    [self clickedCancel:nil];
}

- (void)hide
{
    if (kCSKeyWindow.ordCurCondView)
    {
        @weakify(self);
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            self.alpha = 0;
            kCSKeyWindow.ordCurCondMaskView.alpha = 0;
        } completion:^(BOOL finished) {
//            @strongify(self);
//            if (self.completionBlock)
//            {
//                self.completionBlock(nil);
//            }
            [kCSKeyWindow.ordCurCondView removeFromSuperview];
            [kCSKeyWindow.ordCurCondMaskView removeFromSuperview];
            kCSKeyWindow.ordCurCondMaskView = nil;
            kCSKeyWindow.ordCurCondView = nil;
        }];
    }
}

@end
